/**
 * 递归树结构
 * @param {Array} arr 数组
 * @param {String|Number} id ID
 * @param {Array} list 组成新数组
 * @returns [{...children:[{...children:[{...}]}]}]
 */
export function treeFn(arr = [], id, list = []) {
  arr.forEach(item => {
    item.label = item.name;
    item.checked = false;
    if (item.parentId == id) {
      list.push(item);
    }
  });
  list.forEach(item => {
    item.children = [];
    treeFn(arr, item.id, item.children);
    if (item.children.length < 1) {
      delete item.children;
    }
  });
  return list;
}
/**
 * 递归设置参数
 * @param {Array} arr 数组
 * @param {String} key 数组的键值 例：children
 * @param {String} field 需要设置的字段 例：checked
 * @param {String|Number|Array|Object|Boolean} val 需要设置的内容 例：true
 */
export function treeSetParmas(arr = [], key, field, val) {
  arr.forEach(item => {
    item[field] = val;
    if (item[key] && item[key].length > 0) {
      treeSetParmas(item[key], key, field, val)
    }
  })
}
/**
 * 重组树形数据
 * @param {Array} arr 数组
 * @param {String} field 需要设置的键值
 */
export function reTreeFn(arr = [], field) {
  arr.forEach(item => {
    item.label = item.name || '';
    item.value = item.path || '';
    item.children = item[field];
    if (item.children && item.children.length > 0) {
      reTreeFn(item.children, field)
    }
  })
}
/**
* 秒转换成时分秒
* @param {number} seconds 需要转换的秒数 
* @returns 时：分：秒
*/
export function convertSecondsToHMS(seconds, format = 'H:i:s') {
  var hours = Math.floor(seconds / 3600);
  var minutes = Math.floor((seconds % 3600) / 60);
  var remainingSeconds = parseInt(seconds % 60);
  hours = hours < 10 ? '0' + hours : hours;
  minutes = minutes < 10 ? '0' + minutes : minutes;
  remainingSeconds = remainingSeconds < 10 ? '0' + remainingSeconds : remainingSeconds;
  if (hours > 0) {
    // return hours + ":" + minutes + ":" + remainingSeconds;
    return format.replace('H', hours).replace('i', minutes).replace('s', remainingSeconds)
  }
  // return minutes + ":" + remainingSeconds;
  return format.replace('H:', '').replace('i', minutes).replace('s', remainingSeconds)
}
/**
 * 时间戳转化为年 月 日 时 分 秒
 * @param {number} number: 传入时间戳
 * @param {string} format：返回格式，支持自定义，但参数必须与formateArr里保持一致
 */
export const formatTime = (number, format = 'Y-m-d H:i:s') => {
  var formateArr = ['Y', 'm', 'd', 'H', 'i', 's'];
  var returnArr = [];
  var date = new Date(number);
  // var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));
  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}
// 前缀加零
function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/**
 * 正则格式
 */
export const regName = /^[\u4E00-\u9FA5]{2,}$/;
export const regPhone = /^1[3456789]\d{9}$/;
export const regEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
export const regIDcard = /^[1-9]\d{5}(18|19|20|21|22)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/;
export const regEnum = /^[A-Za-z0-9]+$/;